package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.MesSaleOrderDetailRpcService;
import com.hunttown.mes.rpc.domain.MesSaleOrderDetailDTO;
import com.hunttown.mes.rpc.domain.query.MesSaleOrderDetailDTOQuery;
import com.hunttown.mes.service.MesSaleOrderDetailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.MesSaleOrderDetailConverts.convert;

/**
 * created by wangjunfu on 2022-03-30 15:36:01
 */
@Service
public class MesSaleOrderDetailRpcServiceImpl implements MesSaleOrderDetailRpcService {

    private final Logger logger = LoggerFactory.getLogger(MesSaleOrderDetailRpcServiceImpl.class);

    private final MesSaleOrderDetailService mesSaleOrderDetailService;

    @Autowired
    public MesSaleOrderDetailRpcServiceImpl(MesSaleOrderDetailService mesSaleOrderDetailService) {
        this.mesSaleOrderDetailService = mesSaleOrderDetailService;
    }

    @Override
    public MesSaleOrderDetailDTO insert(MesSaleOrderDetailDTO mesSaleOrderDetailDTO) {
        try {
            return convert(mesSaleOrderDetailService.insert(convert(mesSaleOrderDetailDTO)));
        } catch (Exception e) {
            logger.error("MesSaleOrderDetail create failed.", e);
            throw new RuntimeException("MesSaleOrderDetail create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(MesSaleOrderDetailDTO mesSaleOrderDetailDTO) {
        try {
            return mesSaleOrderDetailService.updateInfoById(convert(mesSaleOrderDetailDTO));
        } catch (Exception e) {
            logger.error("MesSaleOrderDetail update by id failed.", e);
            throw new RuntimeException("MesSaleOrderDetail update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(MesSaleOrderDetailDTOQuery mesSaleOrderDetailDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (mesSaleOrderDetailDTOQuery.getW_id() != null && mesSaleOrderDetailDTOQuery.getW_id() > 0) {
                query.put("w_id", mesSaleOrderDetailDTOQuery.getW_id());
            }

            if (mesSaleOrderDetailDTOQuery.getW_orderId() != null && mesSaleOrderDetailDTOQuery.getW_orderId() > 0) {
                query.put("w_orderId", mesSaleOrderDetailDTOQuery.getW_orderId());
            }

            if (mesSaleOrderDetailDTOQuery.getIds() != null && !mesSaleOrderDetailDTOQuery.getIds().equals("")) {
                query.put("ids", mesSaleOrderDetailDTOQuery.getIds());
            }

            if (mesSaleOrderDetailDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", mesSaleOrderDetailDTOQuery.getStopFlag());
            }

            if (mesSaleOrderDetailDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", mesSaleOrderDetailDTOQuery.getDeleteFlag());
            }

            return mesSaleOrderDetailService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("MesSaleOrderDetail update by query failed.", e);
            throw new RuntimeException("MesSaleOrderDetail update by query failed, Please order your code.", e);
        }
    }

    @Override
    public MesSaleOrderDetailDTO selectById(Integer id) {
        try {
            return convert(mesSaleOrderDetailService.selectById(id));
        } catch (Exception e) {
            logger.error("MesSaleOrderDetail select by id failed.", e);
            throw new RuntimeException("MesSaleOrderDetail select by id failed, Please order your code.", e);
        }
    }

    @Override
    public MesSaleOrderDetailDTO selectObjByQuery(MesSaleOrderDetailDTOQuery mesSaleOrderDetailDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (mesSaleOrderDetailDTOQuery.getOrderId() != null) {
                query.put("orderId", mesSaleOrderDetailDTOQuery.getOrderId());
            }

            if (mesSaleOrderDetailDTOQuery.getProductId() != null) {
                query.put("productId", mesSaleOrderDetailDTOQuery.getProductId());
            }

            if (mesSaleOrderDetailDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", mesSaleOrderDetailDTOQuery.getStopFlag());
            }

            if (mesSaleOrderDetailDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", mesSaleOrderDetailDTOQuery.getDeleteFlag());
            }

            if (StringUtils.isNotBlank(mesSaleOrderDetailDTOQuery.getOrderBy())) {
                query.put("orderBy", mesSaleOrderDetailDTOQuery.getOrderBy());
            }

            return convert(mesSaleOrderDetailService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("MesSaleOrderDetail select obj by query failed.", e);
            throw new RuntimeException("MesSaleOrderDetail select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<MesSaleOrderDetailDTO> getListForPage(MesSaleOrderDetailDTOQuery mesSaleOrderDetailDTOQuery) {
        Query query = Query.page(mesSaleOrderDetailDTOQuery.getPn(), mesSaleOrderDetailDTOQuery.getPageSize());

        if (mesSaleOrderDetailDTOQuery.getId() != null && mesSaleOrderDetailDTOQuery.getId() > 0) {
            query.put("id", mesSaleOrderDetailDTOQuery.getId());
        }

        if (mesSaleOrderDetailDTOQuery.getIds() != null && !mesSaleOrderDetailDTOQuery.getIds().equals("")) {
            query.put("ids", mesSaleOrderDetailDTOQuery.getIds());
        }

        if (mesSaleOrderDetailDTOQuery.getOrderId() != null) {
            query.put("orderId", mesSaleOrderDetailDTOQuery.getOrderId());
        }

        if (StringUtils.isNotBlank(mesSaleOrderDetailDTOQuery.getLikeName())) {
            query.put("likeName", mesSaleOrderDetailDTOQuery.getLikeName());
        }

        if (StringUtils.isNotBlank(mesSaleOrderDetailDTOQuery.getShowDate())) {
            query.put("showDate", mesSaleOrderDetailDTOQuery.getShowDate());
        }

        if (StringUtils.isNotBlank(mesSaleOrderDetailDTOQuery.getStartDate())) {
            query.put("startDate", mesSaleOrderDetailDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(mesSaleOrderDetailDTOQuery.getEndDate())) {
            query.put("endDate", mesSaleOrderDetailDTOQuery.getEndDate());
        }

        if (mesSaleOrderDetailDTOQuery.getStopFlag() != null) {
            query.put("stopFlag", mesSaleOrderDetailDTOQuery.getStopFlag());
        }

        if (mesSaleOrderDetailDTOQuery.getDeleteFlag() != null) {
            query.put("deleteFlag", mesSaleOrderDetailDTOQuery.getDeleteFlag());
        }

        if (StringUtils.isNotBlank(mesSaleOrderDetailDTOQuery.getOrderBy())) {
            query.put("orderBy", mesSaleOrderDetailDTOQuery.getOrderBy());
        }

        List<MesSaleOrderDetailDTO> list = convert(mesSaleOrderDetailService.selectByQuery(query));
        Page<MesSaleOrderDetailDTO> page = Page.from(mesSaleOrderDetailDTOQuery.getPn(), mesSaleOrderDetailDTOQuery.getPageSize(), list);
        page.setTotalItems(mesSaleOrderDetailService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return mesSaleOrderDetailService.deleteById(id);
        } catch (Exception e) {
            logger.error("MesSaleOrderDetail delete failed.", e);
            throw new RuntimeException("MesSaleOrderDetail delete failed, Please order your code.", e);
        }
    }

    @Override
    public Long getProductProduceCount(Integer productId) {
        try {
            return mesSaleOrderDetailService.getProductProduceCount(productId);
        } catch (Exception e) {
            logger.error("MesSaleOrderDetail get product produce count.", e);
            throw new RuntimeException("MesSaleOrderDetail get product produce count, Please order your code.", e);
        }
    }
}